/**   
* @Title: LookupServiceImpl.java 
* @Package com.nebula.upms.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author zhanghui   
* @date 2016年2月26日 上午9:41:25 
* @version V1.0   
*/
package com.nebula.upms.service.biz.impl;

import java.util.List;
import java.util.Map;

import com.nebula.common.datasource.mybatis.pager.DataPager;
import com.nebula.common.datasource.mybatis.pager.Pager;
import com.nebula.upms.service.biz.LookupService;
import com.nebula.upms.service.model.po.UpmsLookupCodePO;
import com.nebula.upms.service.model.po.UpmsLookupCodePOExample;
import com.nebula.upms.service.model.po.UpmsLookupTypePO;
import com.nebula.upms.service.model.vo.LookupVO;
import com.nebula.upms.service.support.datasource.UpmsDs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nebula.common.framework.BaseService;


/** 
* @ClassName: LookupServiceImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author zhanghui
* @date 2016年2月26日 上午9:41:25 
*  
*/
@Service("lookupService")
@Transactional
public class LookupServiceImpl extends BaseService implements LookupService {
	@Override
	public DataPager<LookupVO> getLookupVOListForPager(Map<String, Object> params, Pager pager) {
		/**
		for (int i = 0; i <200; i++) {
			CuCachePO cuCachePO=new CuCachePO();
			cuCachePO.setCacheId(KeyCodeGenerator.random32UUID());
			this.commDAO.initDAO().insert(cuCachePO);
			
			DataPager<LookupVO> dp=this.commDAO.readDB().initDAO("PrivilegeMapper").selectByPage("selectLookupVOList", params, pager);
			
			cuCachePO.setCacheId(KeyCodeGenerator.random32UUID());
			this.commDAO.initDAO().insert(cuCachePO);
		}
		**/
		DataPager<LookupVO> dp= UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectDataPager("selectLookupVOList", params, pager);
		return dp;
	}

	@Override
	public boolean isLookupTypeExist(String lookupType, String oldLookupType) {
		// TODO Auto-generated method stub
		return false;
	}

	
	@Override
	public boolean isLookupCodeExist(String lookupCode, String oldLookupCode) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	* Description:
	* @param upmsLookupTypePO
	* @param lookupCodeArray
	* @param codeNameArray
	* @param groupCodeArray
	**/
	@Override
	public void saveLookup(UpmsLookupTypePO upmsLookupTypePO, String[] lookupCodeArray, String[] codeNameArray,
						   String[] groupCodeArray) {
		UpmsDs.instance().mybatis().ds().mapper().insert(upmsLookupTypePO);
		if(null!=lookupCodeArray){
			for (int i = 0; i < lookupCodeArray.length; i++) {
				UpmsLookupCodePO upmsLookupCodePO=new UpmsLookupCodePO();
				upmsLookupCodePO.setLookupType(upmsLookupTypePO.getLookupType());
				upmsLookupCodePO.setLookupCode(lookupCodeArray[i]);
				upmsLookupCodePO.setGroupCode(groupCodeArray[i]);
				upmsLookupCodePO.setCodeName(codeNameArray[i]);
				upmsLookupCodePO.setPriority(i);
				UpmsDs.instance().mybatis().ds().mapper().insert(upmsLookupCodePO);
			}
		}
	}

	/**
	* Description:
	* @param lookupType
	* @return
	**/
	@Override
	public UpmsLookupTypePO getUpmsLookupTypePO(String lookupType) {
		UpmsLookupTypePO upmsLookupTypePO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsLookupTypePO.class, lookupType);
		return upmsLookupTypePO;
	}

	/**
	* Description:
	* @param lookupType
	* @return
	**/
	@Override
	public List<UpmsLookupCodePO> getUpmsLookupCodePOList(String lookupType) {
		UpmsLookupCodePOExample example=new UpmsLookupCodePOExample();
		example.createCriteria().andLookupTypeEqualTo(lookupType);
		List<UpmsLookupCodePO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
		return list;
	}

	/**
	* Description:
	* @param upmsLookupTypePO
	* @param lookupCodeArray
	* @param codeNameArray
	* @param groupCodeArray
	**/
	@Override
	public void updateLookup(UpmsLookupTypePO upmsLookupTypePO, String[] lookupCodeArray, String[] codeNameArray,
			String[] groupCodeArray) {
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(upmsLookupTypePO);
		UpmsLookupCodePOExample example=new UpmsLookupCodePOExample();
		example.createCriteria().andLookupTypeEqualTo(upmsLookupTypePO.getLookupType());

		UpmsDs.instance().mybatis().ds().mapper().deleteByExample(example);
		if(null!=lookupCodeArray){
			for (int i = 0; i < lookupCodeArray.length; i++) {
				UpmsLookupCodePO upmsLookupCodePO=new UpmsLookupCodePO();
				upmsLookupCodePO.setLookupType(upmsLookupTypePO.getLookupType());
				upmsLookupCodePO.setLookupCode(lookupCodeArray[i]);
				if(null!=groupCodeArray&&groupCodeArray.length>0){
					upmsLookupCodePO.setGroupCode(groupCodeArray[i]);
				}
				upmsLookupCodePO.setCodeName(codeNameArray[i]);
				upmsLookupCodePO.setPriority(i);
				UpmsDs.instance().mybatis().ds().mapper().insert(upmsLookupCodePO);
			}
		}
	}

	/**
	* Description:
	* @param lookupType
	**/
	@Override
	public void removeLookupType(String lookupType) {
		UpmsLookupCodePOExample example=new UpmsLookupCodePOExample();
		example.createCriteria().andLookupTypeEqualTo(lookupType);
		UpmsDs.instance().mybatis().ds().mapper().deleteByExample(example);

		UpmsDs.instance().mybatis().ds().mapper().deleteByPrimaryKey(UpmsLookupTypePO.class, lookupType);
		
	}

	/**
	* Description:
	* @param lookupCode
	**/
	@Override
	public void removeLookupCode(String lookupCode) {
		UpmsDs.instance().mybatis().ds().mapper().deleteByPrimaryKey(UpmsLookupCodePO.class, lookupCode);
	}
}
